package com.huawei.apig.sdk.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.sql.*;

/**
 * @author 刘欣
 * @createTime 2023/8/14 23:28
 * @description
 */
@Component
@Slf4j
public class JDBCUtils {
    //客户正式环境数据库
    private static final String URL = "jdbc:mysql://127.0.0.1:3306";
    private static final String USERNAME = "kk";
    private static final String PASSWORD = "1357!@Qetu";

    public Connection getConnection() throws SQLException, ClassNotFoundException {
        log.info("获取数据库连接");
        log.info("USERNAME:{},PASSWORD:{}",USERNAME,PASSWORD);
        Class.forName("com.mysql.cj.jdbc.Driver");
        return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }

    public static void close(Connection conn, Statement stmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
